package com.league.zjxn.main.mine

import android.os.Bundle
import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.UserViewModel
import com.league.zjxn.main.mine.adapter.FollowEnterpriseAdapter
import com.league.zjxn.main.mine.adapter.FollowProjectAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_home_trend.*
import kotlinx.android.synthetic.main.fragment_mine_follow.*

class MineFollowFragment : OwnerViewModeFragment<UserViewModel>() {

    companion object {
        private const val KEY_FOLLOW_TYPE = "KEY_FOLLOW_TYPE"
        fun newInstance(type: Int): MineFollowFragment {
            val bundle: Bundle = Bundle().apply {
                putInt(KEY_FOLLOW_TYPE, type)
            }
            return MineFollowFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mFollowEnterpriseAdapter = FollowEnterpriseAdapter()

    private val mFollowProjectAdapter = FollowProjectAdapter()

    private val mType: Int by lazy {
        val type = arguments?.getInt(KEY_FOLLOW_TYPE) ?: 0
        if (type == 1) 2 else if (type == 2) 3 else if (type == 3) 1 else 5
    }

    override fun viewModel(): UserViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_mine_follow

    override fun initView() {
        rvMineFollowBody.adapter = if (mType == 5) {
            mFollowEnterpriseAdapter
        } else {
            mFollowProjectAdapter
        }
    }

    override fun initData() {
        if (mType == 5) {
            viewModel.userFollowEnterprise(mType.toString())
        } else {
            viewModel.userFollowProject(mType.toString())
        }
    }

    override fun initViewMode() {
        viewModel.followEnterpriseLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data?.data
                ?: return@observe
            mFollowEnterpriseAdapter.setNewInstance(data)
        })
        viewModel.followProjectLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data?.data
                ?: return@observe
            mFollowProjectAdapter.setNewInstance(data)
        })
    }

}